<?php
class ControllerShippingSpeedy extends Controller {
	private $error = array();
	private $speedy;

	public function __construct($registry) {
		parent::__construct($registry);
		require_once(DIR_SYSTEM . 'library/speedy.php');
		$this->speedy = new Speedy($registry);
	}

	public function index() {
		$this->language->load('shipping/speedy');

		$this->load->model('shipping/speedy');

		$results = array();

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			if ($this->customer->isLogged() && isset($this->session->data['shipping_address_id'])) {
				$this->model_shipping_speedy->addAddress($this->session->data['shipping_address_id'], $this->request->post);
			}

			$this->session->data['speedy'] = $this->request->post;

			$results['submit'] = true;

			$this->response->setOutput(json_encode($results));
		}

		if ((!$this->cart->hasProducts() && (!isset($this->session->data['vouchers']) || !$this->session->data['vouchers'])) || (!$this->cart->hasStock() && !$this->config->get('config_stock_checkout'))) {
			$results['redirect'] = $this->url->link('checkout/cart');

			$this->response->setOutput(json_encode($results));
		}

		if (!$this->customer->isLogged() && !isset($this->session->data['guest'])) {
			$results['redirect'] = $this->url->link('checkout/checkout', '', 'SSL');

			$this->response->setOutput(json_encode($results));
		}

		$this->data['text_calculate'] = $this->language->get('text_calculate');
		$this->data['text_wait'] = $this->language->get('text_wait');
		$this->data['text_yes'] = $this->language->get('text_yes');
		$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_to_office'] = $this->language->get('text_to_office');
		$this->data['text_to_door'] = $this->language->get('text_to_door');
		$this->data['text_select_city'] = $this->language->get('text_select_city');
		$this->data['text_fixed_time'] = $this->language->get('text_fixed_time');
		$this->data['text_modify'] = $this->language->get('text_modify');

		$this->data['entry_cod'] = $this->language->get('entry_cod');
		$this->data['entry_shipping_to'] = $this->language->get('entry_shipping_to');
		$this->data['entry_postcode'] = $this->language->get('entry_postcode');
		$this->data['entry_city'] = $this->language->get('entry_city');
		$this->data['entry_quarter'] = $this->language->get('entry_quarter');
		$this->data['entry_street'] = $this->language->get('entry_street');
		$this->data['entry_street_no'] = $this->language->get('entry_street_no');
		$this->data['entry_block_no'] = $this->language->get('entry_block_no');
		$this->data['entry_entrance_no'] = $this->language->get('entry_entrance_no');
		$this->data['entry_floor_no'] = $this->language->get('entry_floor_no');
		$this->data['entry_apartment_no'] = $this->language->get('entry_apartment_no');
		$this->data['entry_office'] = $this->language->get('entry_office');
		$this->data['entry_note'] = $this->language->get('entry_note');
		$this->data['entry_fixed_time'] = $this->language->get('entry_fixed_time');

		$this->data['button_calculate'] = $this->language->get('button_calculate');

		if (isset($this->error['address'])) {
			$this->data['error_address'] = $this->error['address'];
		} else {
			$this->data['error_address'] = '';
		}

		if (isset($this->error['office'])) {
			$this->data['error_office'] = $this->error['office'];
		} else {
			$this->data['error_office'] = '';
		}

		if (isset($this->error['fixed_time'])) {
			$this->data['error_fixed_time'] = $this->error['fixed_time'];
		} else {
			$this->data['error_fixed_time'] = '';
		}

		if (isset($this->error['cod'])) {
			$this->data['error_cod'] = $this->error['cod'];
		} else {
			$this->data['error_cod'] = '';
		}

		$this->data['action'] = $this->url->link('shipping/speedy', '', 'SSL');

		if ($this->customer->isLogged() && isset($this->session->data['shipping_address_id'])) {
			$shipping_address = $this->model_shipping_speedy->getAddress($this->session->data['shipping_address_id']);

			$this->session->data['speedy_customer']['shipping_address_id'] = $this->session->data['shipping_address_id'];
		} elseif (isset($this->session->data['guest'])) {
			if (isset($this->session->data['speedy']) && isset($this->session->data['speedy_guest']) &&
				$this->session->data['speedy_guest']['city'] == $this->session->data['guest']['shipping']['city'] &&
				$this->session->data['speedy_guest']['postcode'] == $this->session->data['guest']['shipping']['postcode']) {
				$shipping_address = $this->session->data['speedy'];
			}

			$this->session->data['speedy_guest']['city'] = $this->session->data['guest']['shipping']['city'];
			$this->session->data['speedy_guest']['postcode'] = $this->session->data['guest']['shipping']['postcode'];
		}

		if (!$this->config->get('speedy_cod_status')) {
			$this->data['cod'] = false;
		} elseif (isset($this->request->post['cod'])) {
			$this->data['cod'] = $this->request->post['cod'];
		} elseif (isset($this->session->data['speedy']['cod'])) {
			$this->data['cod'] = $this->session->data['speedy']['cod'];
		} else {
			$this->data['cod'] = null;
		}

		if (isset($this->request->post['to_office'])) {
			$this->data['to_office'] = $this->request->post['to_office'];
		} elseif (isset($shipping_address['to_office'])) {
			$this->data['to_office'] = $shipping_address['to_office'];
		} else {
			$this->data['to_office'] = 0;
		}

		if (isset($this->request->post['postcode'])) {
			$this->data['postcode'] = $this->request->post['postcode'];
		} elseif (isset($shipping_address['postcode'])) {
			$this->data['postcode'] = $shipping_address['postcode'];
		} else {
			$this->data['postcode'] = '';
		}

		if (isset($this->request->post['city'])) {
			$this->data['city'] = $this->request->post['city'];
		} elseif (isset($shipping_address['city'])) {
			$this->data['city'] = $shipping_address['city'];
		} else {
			$this->data['city'] = '';
		}

		if (isset($this->request->post['city_id'])) {
			$this->data['city_id'] = $this->request->post['city_id'];
		} elseif (isset($shipping_address['city_id'])) {
			$this->data['city_id'] = $shipping_address['city_id'];
		} else {
			$this->data['city_id'] = 0;
		}

		if (isset($this->request->post['city_nomenclature'])) {
			$this->data['city_nomenclature'] = $this->request->post['city_nomenclature'];
		} elseif (isset($shipping_address['city_nomenclature'])) {
			$this->data['city_nomenclature'] = $shipping_address['city_nomenclature'];
		} else {
			$this->data['city_nomenclature'] = '';
		}

		if (isset($this->request->post['quarter'])) {
			$this->data['quarter'] = $this->request->post['quarter'];
		} elseif (isset($shipping_address['quarter'])) {
			$this->data['quarter'] = $shipping_address['quarter'];
		} else {
			$this->data['quarter'] = '';
		}

		if (isset($this->request->post['quarter_id'])) {
			$this->data['quarter_id'] = $this->request->post['quarter_id'];
		} elseif (isset($shipping_address['quarter_id'])) {
			$this->data['quarter_id'] = $shipping_address['quarter_id'];
		} else {
			$this->data['quarter_id'] = 0;
		}

		if (isset($this->request->post['street'])) {
			$this->data['street'] = $this->request->post['street'];
		} elseif (isset($shipping_address['street'])) {
			$this->data['street'] = $shipping_address['street'];
		} else {
			$this->data['street'] = '';
		}

		if (isset($this->request->post['street_id'])) {
			$this->data['street_id'] = $this->request->post['street_id'];
		} elseif (isset($shipping_address['street_id'])) {
			$this->data['street_id'] = $shipping_address['street_id'];
		} else {
			$this->data['street_id'] = 0;
		}

		if (isset($this->request->post['street_no'])) {
			$this->data['street_no'] = $this->request->post['street_no'];
		} elseif (isset($shipping_address['street_no'])) {
			$this->data['street_no'] = $shipping_address['street_no'];
		} else {
			$this->data['street_no'] = '';
		}

		if (isset($this->request->post['block_no'])) {
			$this->data['block_no'] = $this->request->post['block_no'];
		} elseif (isset($shipping_address['block_no'])) {
			$this->data['block_no'] = $shipping_address['block_no'];
		} else {
			$this->data['block_no'] = '';
		}

		if (isset($this->request->post['entrance_no'])) {
			$this->data['entrance_no'] = $this->request->post['entrance_no'];
		} elseif (isset($shipping_address['entrance_no'])) {
			$this->data['entrance_no'] = $shipping_address['entrance_no'];
		} else {
			$this->data['entrance_no'] = '';
		}

		if (isset($this->request->post['floor_no'])) {
			$this->data['floor_no'] = $this->request->post['floor_no'];
		} elseif (isset($shipping_address['floor_no'])) {
			$this->data['floor_no'] = $shipping_address['floor_no'];
		} else {
			$this->data['floor_no'] = '';
		}

		if (isset($this->request->post['apartment_no'])) {
			$this->data['apartment_no'] = $this->request->post['apartment_no'];
		} elseif (isset($shipping_address['apartment_no'])) {
			$this->data['apartment_no'] = $shipping_address['apartment_no'];
		} else {
			$this->data['apartment_no'] = '';
		}

		if (isset($this->request->post['office_id'])) {
			$this->data['office_id'] = $this->request->post['office_id'];
		} elseif (isset($shipping_address['office_id'])) {
			$this->data['office_id'] = $shipping_address['office_id'];
		} else {
			$this->data['office_id'] = 0;
		}

		if (isset($this->request->post['note'])) {
			$this->data['note'] = $this->request->post['note'];
		} elseif (isset($shipping_address['note'])) {
			$this->data['note'] = $shipping_address['note'];
		} else {
			$this->data['note'] = '';
		}

		if (isset($this->request->post['fixed_time_cb'])) {
			$this->data['fixed_time_cb'] = $this->request->post['fixed_time_cb'];
		} elseif (isset($this->session->data['speedy']['fixed_time_cb'])) {
			$this->data['fixed_time_cb'] = $this->session->data['speedy']['fixed_time_cb'];
		} else {
			$this->data['fixed_time_cb'] = false;
		}

		if (!isset($this->session->data['speedy']['methods_fixed_time']) || (isset($this->request->post['shipping_method']) && !isset($this->session->data['speedy']['methods_fixed_time'][trim($this->request->post['shipping_method'], 'speedy.')]))) {
			$this->data['fixed_time_cb'] = false;
			$this->data['fixed_time_cb_enable'] = false;
		} else {
			$this->data['fixed_time_cb_enable'] = true;
		}

		if (isset($this->request->post['fixed_time_hour'])) {
			$this->data['fixed_time_hour'] = $this->request->post['fixed_time_hour'];
		} elseif (isset($this->session->data['speedy']['fixed_time_hour'])) {
			$this->data['fixed_time_hour'] = $this->session->data['speedy']['fixed_time_hour'];
		} else {
			$this->data['fixed_time_hour'] = '';
		}

		if (isset($this->request->post['fixed_time_min'])) {
			$this->data['fixed_time_min'] = $this->request->post['fixed_time_min'];
		} elseif (isset($this->session->data['speedy']['fixed_time_min'])) {
			$this->data['fixed_time_min'] = $this->session->data['speedy']['fixed_time_min'];
		} else {
			$this->data['fixed_time_min'] = '';
		}

		$this->data['fixed_time'] = $this->config->get('speedy_fixed_time');

		$this->data['offices'] = array();

		if (!$this->data['city_id']) {
			if ($this->customer->isLogged() && isset($this->session->data['shipping_address_id'])) {
				$this->load->model('account/address');

				$shipping_address = $this->model_account_address->getAddress($this->session->data['shipping_address_id']);
			} elseif (isset($this->session->data['guest'])) {
				$shipping_address = $this->session->data['guest']['shipping'];
			}

			$cities = $this->speedy->getCities($shipping_address['city'], $shipping_address['postcode']);

			if (!$this->speedy->getError()) {
				if (count($cities) == 1) {
					$this->data['postcode'] = $cities[0]['postcode'];
					$this->data['city'] = $cities[0]['value'];
					$this->data['city_id'] = $cities[0]['id'];
					$this->data['city_nomenclature'] = $cities[0]['nomenclature'];
				}
			} else {
				$this->data['error_address'] = $this->speedy->getError();
			}
		}

		if ($this->data['city_id']) {
			$this->data['offices'] = $this->speedy->getOffices(null, $this->data['city_id']);

			if ($this->speedy->getError()) {
				$this->data['error_office'] = $this->speedy->getError();
			}
		}

		$this->data['cod_status'] = $this->config->get('speedy_cod_status');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/shipping/speedy.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/shipping/speedy.tpl';
		} else {
			$this->template = 'default/template/shipping/speedy.tpl';
		}

		$results['html'] = $this->render();

		$this->response->setOutput(json_encode($results));
	}

	protected function validate() {
		if ($this->request->post['postcode'] && $this->request->post['city'] && $this->request->post['city_id'] &&
			(!$this->request->post['to_office'] && (($this->request->post['quarter'] && ($this->request->post['quarter_id'] && $this->request->post['city_nomenclature'] == 'FULL' || $this->request->post['city_nomenclature'] != 'FULL') && ($this->request->post['block_no'] || $this->request->post['street_no'])) ||
			($this->request->post['street'] && ($this->request->post['street_id'] && $this->request->post['city_nomenclature'] == 'FULL' || $this->request->post['city_nomenclature'] != 'FULL') && ($this->request->post['block_no'] || $this->request->post['street_no'])) || $this->request->post['note']) || ($this->request->post['to_office'] && $this->request->post['office_id']))) {
		} else {
			if ($this->request->post['to_office']) {
				$this->error['office'] = $this->language->get('error_office');
			} else {
				$this->error['address'] = $this->language->get('error_address');
			}
		}

		if (isset($this->request->post['fixed_time_cb'])) {
			if (!$this->request->post['fixed_time_hour'] || $this->request->post['fixed_time_hour'] < 10 || $this->request->post['fixed_time_hour'] > 17 ||
				!$this->request->post['fixed_time_min'] || $this->request->post['fixed_time_min'] < '00' || $this->request->post['fixed_time_min'] > 59 ||
				($this->request->post['fixed_time_hour'] == 10 && $this->request->post['fixed_time_min'] < 30) ||
				($this->request->post['fixed_time_hour'] == 17 && $this->request->post['fixed_time_min'] > 30)) {
				$this->error['fixed_time'] = $this->language->get('error_fixed_time');
			}
		}

		if (!isset($this->request->post['cod'])) {
			$this->error['cod'] = $this->language->get('error_cod');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	public function getCities() {
		if (isset($this->request->get['term'])) {
			$name = $this->request->get['term'];
		} else {
			$name = '';
		}

		$data = $this->speedy->getCities($name);

		if ($this->speedy->getError()) {
			$data = array('error' => $this->speedy->getError());
		}

		$this->response->setOutput(json_encode($data));
	}

	public function getQuarters() {
		if (isset($this->request->get['term'])) {
			$name = $this->request->get['term'];
		} else {
			$name = '';
		}

		if (isset($this->request->get['city_id'])) {
			$city_id = $this->request->get['city_id'];
		} else {
			$city_id = '';
		}

		if ($city_id) {
			$data = $this->speedy->getQuarters($name, $city_id);

			if ($this->speedy->getError()) {
				$data = array('error' => $this->speedy->getError());
			}
		} else {
			$this->language->load('shipping/speedy');
			$data = array('error' => $this->language->get('error_city'));
		}

		$this->response->setOutput(json_encode($data));
	}

	public function getStreets() {
		if (isset($this->request->get['term'])) {
			$name = $this->request->get['term'];
		} else {
			$name = '';
		}

		if (isset($this->request->get['city_id'])) {
			$city_id = $this->request->get['city_id'];
		} else {
			$city_id = '';
		}

		if ($city_id) {
			$data = $this->speedy->getStreets($name, $city_id);

			if ($this->speedy->getError()) {
				$data = array('error' => $this->speedy->getError());
			}
		} else {
			$this->language->load('shipping/speedy');
			$data = array('error' => $this->language->get('error_city'));
		}

		$this->response->setOutput(json_encode($data));
	}

	public function getBlocks() {
		if (isset($this->request->get['term'])) {
			$name = $this->request->get['term'];
		} else {
			$name = '';
		}

		if (isset($this->request->get['city_id'])) {
			$city_id = $this->request->get['city_id'];
		} else {
			$city_id = '';
		}

		if ($city_id) {
			$data = $this->speedy->getBlocks($name, $city_id);

			if ($this->speedy->getError()) {
				$data = array('error' => $this->speedy->getError());
			}
		} else {
			$this->language->load('shipping/speedy');
			$data = array('error' => $this->language->get('error_city'));
		}

		$this->response->setOutput(json_encode($data));
	}

	public function getOffices() {
		if (isset($this->request->get['term'])) {
			$name = $this->request->get['term'];
		} else {
			$name = '';
		}

		if (isset($this->request->get['city_id'])) {
			$city_id = $this->request->get['city_id'];
		} else {
			$city_id = '';
		}

		if ($city_id) {
			$data = $this->speedy->getOffices($name, $city_id);

			if ($this->speedy->getError()) {
				$data = array('error' => $this->speedy->getError());
			}
		} else {
			$this->language->load('shipping/speedy');
			$data = array('error' => $this->language->get('error_city'));
		}

		$this->response->setOutput(json_encode($data));
	}
}
?>
